﻿// owner 蒋家伟

#pragma once
#include <atomic>
#include "ModelBase_home.h"

namespace jsdk
{
    // 所有需要使用WeakPtr的类型都需要从此类继承
    class MODELBASE_EXPORT WeakReference
    {
        template <typename> friend class WeakPtr;
    public:
        virtual ~WeakReference();

    private:
        class MODELBASE_EXPORT Proxy
        {
        public:
            Proxy(WeakReference* pWeakReference);
            virtual ~Proxy();
            bool isValid() const;
            void ref();
            void unRef();
            WeakReference* get() const;
            void reset();
        private:
            std::atomic_int m_count = 0;
            WeakReference* m_pWeakReference = nullptr; // 指代WeakReference
        };

        Proxy* getProxy() const;
        mutable Proxy* m_pProxy = nullptr;

    protected:
        WeakReference();
    };
}